package com.dk.controller;

import com.dk.data.Result;
import com.dk.domain.TCarDTO;
import com.dk.em.ConstantEnum;
import com.dk.entity.BSVo;
import com.dk.entity.TBrand;
import com.dk.entity.TCarSeries;
import com.dk.feign.CarSeekFeign;
import com.dk.mapper.TBrandMapper;
import com.dk.mapper.TCarSeriesMapper;
import com.dk.service.ITBrandService;
import com.dk.service.ITCarSeriesService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private TBrandMapper brandMapper;

    @Autowired
    private ITCarSeriesService itCarSeriesService;

    @Autowired
    private TCarSeriesMapper carSeriesMapper;

    @Autowired
    private ITBrandService itBrandService;

    @Autowired
    private CarSeekFeign carSeekFeign;

    @Autowired
    private ITBrandService brandService;

    @Autowired
    private ITCarSeriesService seriesService;

    @ApiOperation(value = "查询所有品牌")
    @GetMapping("findAllByBrand")
    public Result findAllByBrand(){
        List<TBrand> tBrands = itBrandService.list();
        return Result.bulid(ConstantEnum.SUCCESS).setResult(tBrands);
    }

    @ApiOperation(value = "查询所有车系")
    @GetMapping("findAllByCarSeries")
    public Result findAllByCarSeries(){
        List<TCarSeries> list = itCarSeriesService.list();
        return Result.bulid(ConstantEnum.SUCCESS).setResult(list);
    }

    @ApiOperation(value = "根据品牌id查询他下面的所有系列")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "品牌Id",required = true,paramType = "query")
    })
    @GetMapping("findAllByBrandId")
    public Result findAllByAlliance(@RequestParam Long id){
      List<TCarSeries> seriesList = carSeriesMapper.findAllByBrandId(id);
      return Result.bulid(ConstantEnum.SUCCESS).setResult(seriesList);
    }

/*    //  根据传过来的品牌id和系列id查询
    @ApiOperation(value = "根据品牌名称调用es查询数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "品牌Id",required = true,paramType = "query")
    })
    @PostMapping("findAllByBrandIdAndSeriesId")
    public Result findAllByBrandIdAndSeriesId(Long brandId,Long seriesId){
        BSVo vo=brandMapper.findAllByBrandIdAndSeriesId(brandId,seriesId);
        String brandName = vo.getBrandName();
//      调用feign里面的es搜索方法根据品牌搜索
        Result<TCarDTO> result = carSeekFeign.getInfoByName(name, 1, 100);
        return Result.bulid(ConstantEnum.SUCCESS).setResult(name);
    }*/

    //  根据传过来的品牌id和系列id查询
    @ApiOperation(value = "根据品牌车系名称调用es查询数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name="brandId",value = "品牌Id",required = true,paramType = "query"),
            @ApiImplicitParam(name="seriesId",value = "车系Id",required = true,paramType = "query")
    })
    @PostMapping("findAllByBrandIdAndSeriesIdTwo")
    public Result findAllByBrandIdAndSeriesIdTwo(@RequestParam("brandId") Long brandId, @RequestParam("seriesId") Long seriesId){
        BSVo vo=brandMapper.findAllByBrandIdAndSeriesId(brandId,seriesId);
        String brandName = vo.getBrandName();
        String seriesName = vo.getSeriesName();
        String bsName = brandName + seriesName;
//      调用feign里面的es搜索方法根据品牌加车系拼接搜索
        Result<List<TCarDTO>> result = carSeekFeign.getInfoByName(bsName, 1, 2);
        List<TCarDTO> list = result.getResult();
        return Result.bulid(ConstantEnum.SUCCESS).setResult(list);
    }
}
